﻿using FastReport.DevComponents.DotNetBar;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using WinFormsApp1.SuYi.Week2WinForm;
using WinFormsApp1.WangHaoQi._02Winfroms.CommonDto;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace WinFormsApp1.WangHaoQi.Week02Winfroms
{
    public partial class LiveAnimalsAddHtml : Form
    {
        public LiveAnimalsAddHtml()
        {
            InitializeComponent();
            ShowLoadCheckIn();
            Select();
            //var datea = DateTime.Now.ToString().Replace("/", "").Replace(" ","").Replace(":","");
            //MessageBox.Show(datea);
        }
        /// <summary>
        /// 点击进场表进行页面跳转，根据进场编号查询进场信息
        /// </summary>
        public async void ShowLoadCheckIn()
        {
            if (string.IsNullOrEmpty(CheckInNoSelect.CheckInNo))
            {
                MessageBox.Show("请选择进场编号");
                return;
            }
            else
            {
                MessageBox.Show(CheckInNoSelect.CheckInNo);
                var data = new
                {
                    CheckInNo = CheckInNoSelect.CheckInNo
                };
                var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<CheckIna>>>(URLCommon.Url2,"/LiveAnimals/CheckInNoSelect", HttpClientHelper.MethodType.POST, data);
                var list = items.Data.ToList();
                foreach (var item in list)
                {
                    uiTextBox6.Text = item.CheckInNo;
                    uiLabel15.Text = item.CarNo;
                    uiLabel17.Text = item.SupplierName;
                    uiLabel21.Text = item.OriginAddr;
                    uiLabel19.Text = item.SettlementTypeName;
                    uiLabel25.Text = item.ApproachNum.ToString();
                    uiLabel23.Text = "A" + item.CheckInNo;
                    ScaleZNum.Text = "121";
                    ScaleJNum.Text = (Convert.ToInt32(ScaleZNum.Text) - Convert.ToInt32(ScalePNum.Text)).ToString();
                }
                CheckInNoSelect.CheckInNo = "";
            }

        }

        /// <summary>
        /// 进场登记添加
        /// </summary>
        private async void uiButton2_Click(object sender, EventArgs e)
        {
            //LiveAnimals liveAnimals = new LiveAnimals()
            //{
            //    LiveAnimalsId = 0,
            //    LiveAnimalsDate = Convert.ToDateTime(uiDatetimePicker1.Text),
            //    LiveAnimalsDocument = uiTextBox1.Text,
            //    LiveAnimalsWeight = uiTextBox2.Text,
            //    CheckInId = Convert.ToInt32(uiComboBox1.SelectedValue),
            //    LiveAnimalsRAverageWeight = "",
            //    IsDelete = false
            //};
            //var res = await HttpClientHelper.RequestUrl<LiveAnimals, ApiResult<LiveAnimals>>("http://localhost:5159/api/LiveAnimals/", "LiveAnimalsAdd", HttpClientHelper.MethodType.POST, liveAnimals);
            //if (res.Code == 1)
            //{
            //    //弹框提示
            //    MessageBox.Show("添加成功");
            //    //创建新页面
            //    //BusinesWinFormShow queryshow = new BusinesWinFormShow();
            //    //显示 创建的新页面
            //    //queryshow.Show();
            //    //关闭本页面
            //    this.Hide();
            //}
        }
        public async void Select()
        {
            var data = new
            {

            };
            var res = await HttpClientHelper.RequestUrl<object, ApiResult<List<Waiting>>>(URLCommon.Url2, "/LiveAnimals/WaitingsSelectShow", HttpClientHelper.MethodType.POST, data);
            if (res.Data != null)
            {
                uiComboBox1.DataSource = res.Data;
                uiComboBox1.DisplayMember = "WaitingName";
                uiComboBox1.ValueMember = "WaitingId";
            }
        }
        /// <summary>
        /// 绑定入圈需要的字段
        /// </summary>
        public class Waiting
        {
            public int WaitingId { get; set; }
            public string WaitingName { get; set; }
        }


        //进场登记
        private void uiButton1_Click(object sender, EventArgs e)
        {
            CheckInAddHtml checkInAddHtml = new CheckInAddHtml();

        }

        private void uiButton2_Click_1(object sender, EventArgs e)
        {
            CheckInNoSelect.CheckInNo = "";
            MessageBox.Show("请重新选择进场编号");
            this.Hide();
            return;
        }
        /// <summary>
        /// 磅秤下拉绑定
        /// </summary>
        public class Scale
        {
            public int scaleId { get; set; }
            public string scaleName { get; set; }
            public int TareNum { get; set; }
        }
        public int TareNuma = 0;
        /// <summary>
        /// 选择磅秤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiButton1_Click_2(object sender, EventArgs e)
        {
            if (DepotId > 0)
            {
                uiComboBox2.DataSource = null;
                uiLabel16.Text = "选择磅秤";
                var data = new
                {
                    DepotId = DepotId,
                };
                var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<Scale>>>(URLCommon.Url2, "/Week2Get/ScaleShow", HttpClientHelper.MethodType.POST, data);
                if (items.Data.Count > 0)
                {
                    uiComboBox2.DataSource = items.Data;
                    foreach (var item in items.Data)
                    {
                        TareNuma = item.TareNum;
                    }
                    ScaleJNum.Text = Convert.ToString(Convert.ToInt32(ScaleZNum.Text) - TareNuma);
                    ScalePNum.Text = TareNuma.ToString();
                    uiComboBox2.DisplayMember = "scaleName";
                    uiComboBox2.ValueMember = "scaleId";
                    scaleIdBool = true;
                }
                else
                {
                    MessageBox.Show("该仓库无磅秤请重新选择");
                    uiButton4.Enabled = true;
                    Addnum = 0;
                    DepotId = 0;
                }
                this.Controls.Add(uiComboBox2);
            }
            else
            {
                MessageBox.Show("请先选择仓库");
                return;
            }
        }
        /// <summary>
        /// 选择仓库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiButton4_Click(object sender, EventArgs e)
        {
            Addnum += 1;
            uiLabel16.Text = "选择仓库";
            uiComboBox2.DataSource = null;
            var data = new
            {

            };
            var items = await HttpClientHelper.RequestUrl<object, ApiResult<List<WareHouseData>>>(URLCommon.Url2, "/Week2Get/WarehouseDataShow", HttpClientHelper.MethodType.POST, data);
            uiComboBox2.DataSource = items.Data;
            uiComboBox2.DisplayMember = "depotTypeName";
            uiComboBox2.ValueMember = "depotTypeId";

            this.Controls.Add(uiComboBox2);
        }
        public class WareHouseData
        {
            public int depotTypeId { get; set; }
            public string depotTypeName { get; set; }
        }
        // 仓库
        public int DepotId = 0;
        //判断条件
        public int Addnum = 0;
        public int scaleId = 0;
        public bool scaleIdBool = false;
        private void uiButton9_Click_1(object sender, EventArgs e)
        {
            if (Addnum > 0)
            {
                DepotId = Convert.ToInt32(uiComboBox2.SelectedValue);
                Addnum--;
            }
            if (DepotId > 0)
            {
                scaleId = Convert.ToInt32(uiComboBox2.SelectedValue);
                uiButton4.Enabled = false;
            }
            if (scaleIdBool == true)
            {
                uiButton1.Enabled = false;
            }
        }
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void uiButton3_Click(object sender, EventArgs e)
        {
            LiveAnimals liveAnimals = new LiveAnimals()
            {

                LiveAnimalsId = 0,
                LiveAnimalsDate = DateTime.Now,
                LiveAnimalsDocument = DateTime.Now.ToString().Replace("/", "").Replace(" ", "").Replace(":", ""),
                CheckInNo = uiTextBox6.Text,
                LiveAnimalsWeight = ScaleJNum.Text,
                LiveAnimalsRAverageWeight = ScaleJNum.Text,
                IsDelete = false
            };
            var res = await HttpClientHelper.RequestUrl<LiveAnimals, ApiResult<LiveAnimals>>(URLCommon.Url1, "/LiveAnimals/LiveAnimalsAdd", HttpClientHelper.MethodType.POST, liveAnimals);
            if (res.Code == 1)
            {
                //弹框提示
                MessageBox.Show("添加成功");
                //关闭本页面
                this.Hide();
            }
            else
            {
                //弹框提示
                MessageBox.Show("添加失败");
                //关闭本页面
                this.Hide();
            }
        }
        /// <summary>
        /// 活畜称重单
        /// </summary>
        public class LiveAnimals
        {
            /// <summary>
            /// 活畜称重单Id
            /// </summary>
            [Key]
            public int LiveAnimalsId { get; set; }

            /// <summary>
            /// 日期
            /// </summary>
            public DateTime? LiveAnimalsDate { get; set; }

            /// <summary>
            /// 单据号
            /// </summary>
            public string LiveAnimalsDocument { get; set; }

            /// <summary>
            /// 进场编号
            /// </summary>
            public string CheckInNo { get; set; }

            /// <summary>
            /// 净重
            /// </summary>
            public string LiveAnimalsWeight { get; set; }

            /// <summary>
            /// 均重
            /// </summary>
            public string LiveAnimalsRAverageWeight { get; set; }

            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsDelete { get; set; }
        }
    }
}
